<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/19
 * Time: 11:20
 */
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Shareholder extends  Model
{
    protected $table = 'shareholder';
    public $timestamps = false;

    public static function getRows($where)
    {
        return self::where($where)->first();
    }

    /*
     * 保存股东身份信息
     * */
    public static function updateData($params)
    {
        $model = Shareholder::where([['mobile','=',$params['mobile']]])->orderBy('id', 'desc')->first();
        if(empty($model) || $model->status == 1)
            return ['status'=>'error', 'message'=>'股东账号不存在'];
        if($params['mobile'] != $model->mobile || $params['name'] != $model->user_name)
            return ['status'=>'error', 'message'=>'股东手机号或姓名不匹配'];

        // 身份证不允许修改
        if(empty($model->id_card)){
            if(!is_idcard($params['id_card']))
                return ['status'=>'error', 'message'=>'身份证错误'];
            $model->id_card = $params['id_card'];
        }
            // 校验继承人身份证号
        if(isset($params['inherit_card']) && !empty($params['inherit_card'])){
            if(!is_idcard($params['inherit_card']))
                return ['status'=>'error', 'message'=>'继承人身份证有误'];

        }
        // 校验继承人手机号
        if(isset($params['inherit_mobile']) && !empty($params['inherit_mobile'])){
            if(!preg_match("/^1[345789]\d{9}$/", $params['inherit_mobile']))
                return ['status'=>'error', 'message'=>'继承人手机号有误'];
        }

        // 清空股东证书 协议书
        $model->shareholder_agree_img = "";
        $model->local_agree_img = "";
        $model->shareholder_certificate_img = "";
        $model->local_certificate_img = "";

        $model->inherit_name = $params['inherit_name'];
        $model->inherit_card = $params['inherit_card'];
        $model->inherit_mobile = $params['inherit_mobile'];
        $model->inherit_sex = empty($params['inherit_sex']) ? 0 : (int)$params['inherit_sex'];

        $model->address = $params['address'];
        $model->bank_number = $params['bank_number'];
        $model->bank_name = $params['bank_name'];
        $model->opening_bank = $params['opening_bank'];
        $model->bank_address = $params['bank_address'];

        // 股份状态
        if(!empty($params['operation_status']))
            $model->operation_status = $params['operation_status'];
        $model->updated_at = time2date();

        //正则表达式   -- 匹配是否是反向代理的地址
        $reg = '/(http):\/\/([^\/]+)/i';
        if(isset($params['positive_card'])){
            preg_match($reg, $params['positive_card'],$positive_card_res);
            if(!empty($positive_card_res) && $positive_card_res[0] != env('IMAGE_PROXY_URL'))
                $model->positive_card = $params['positive_card'];
        }
        if(isset($params['opposite_card'])){
            preg_match($reg, $params['opposite_card'],$opposite_card_res);
            if(!empty($opposite_card_res) && $opposite_card_res[0] != env('IMAGE_PROXY_URL'))
                $model->opposite_card = $params['opposite_card'];
        }

        if($model->save()){
            $model->arrival_account = empty($model->arrival_account) ? 0 : strtotime($model->arrival_account);
            return ['status'=>'success', 'message'=>'操作成功', 'data'=>$model];
        }
        return ['status'=>'success', 'message'=>'未知异常'];
    }

}